/**
 * 知识库维护
 * @author yuan_rz
 * @date 2019/12/24
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'ROW_ID', width: 80, title: '序号'}
            , {
                field: 'MMRZ001', width: 150, title: '故障分类', sort: true,
                search: 'default', searchOpts: {icon: true}
            }
            , {field: 'MMRZ001_NAME', width: 150, title: '故障说明', sort: true}
            , {field: 'MMRZ002', width: 150, title: '故障描述', sort: true}
            , {field: 'MMRZ003', width: 150, title: '故障原因', sort: true}
            , {field: 'MMRZ004', width: 150, title: '处理方案', sort: true}
            , {
                field: 'MMRZ005', width: 150, title: '故障图片',
                templet: function (data) {
                    return '<img src="' + '/wissun-smfile/' + data.MMRZ005 + '" style="height: 20px"/>';
                }
            }
            , {
                field: 'MMRZ006', width: 150, title: '技术人员', sort: true, hide: true,
                search: 'default', searchOpts: {icon: true}
            }
            , {field: 'MMRZ006_NAME', width: 150, title: '技术人员', sort: true}
            , {field: 'MMRZ007', width: 150, title: '手机号码', sort: true}
            , {
                field: 'MMRZ008', width: 150, title: '归属部门', sort: true, hide: true,
                search: 'default', searchOpts: {icon: true}
            }
            , {field: 'MMRZ008_NAME', width: 150, title: '归属部门', sort: true}
            , {
                field: 'MMRZACTI', width: 150, title: '有效否', sort: true,
                filterDatas: app.datas_('sys_y_n')
            }
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwai820setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai820SettingAction,
        editArea = ['600px', '625px'],
        moduleName = '知识库'
    ;

    var gwai820 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai820',
                _searchs: [{
                    id: 'gwai820', tableId: 'gwai820_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwai820_edit', tpl: 'gwai820_edit'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai820_table'),
                    url: g_ctxPath + 'crm/gwai820/getListData',
                    importsUrl: g_ctxPath + 'crm/gwai820/importListData',
                    exportsUrl: g_ctxPath + 'crm/gwai820/exportListData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    id: 'gwai820_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports_import','exports'],
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_param,
                    done: function (res, curr, count) {
                        // self.reloadUpload();
                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai820setting-action').innerHTML).render({}, function (string) {
                    gwai820SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai820_table)', function (obj) {
                    switch (obj.event) {
                        case 'detail':
                            layer.tips(gwai820SettingAction, obj.td, {
                                tips: [3, '']
                                , time: 3000
                                , anim: 500
                                , maxWidth: 120
                                , skin: 'layui-table-tips'
                                , success: function (layero, index) {
                                    tipsIndex = index;
                                    layero.find('li').on('click', function () {
                                        switch ($(this).attr('inx')) {
                                            case '1':
                                                self._openTplFull({
                                                    tag: 'gwai820_edit',
                                                    moduleName: moduleName,
                                                    tagData: {
                                                        obj: obj
                                                    },
                                                    index: 0,
                                                    openOpts: {
                                                        title: moduleName + '编辑',
                                                        area: editArea
                                                    }
                                                });
                                                break;
                                            case '2':
                                                self.deleteData(obj);
                                                break;
                                        }
                                        layer.close(index);
                                        tipsIndex = null;
                                    });
                                }
                            });
                            return false;
                            break;
                    }
                });

                //监听行双击事件
                table.on('rowDouble(gwai820_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai820_edit',
                        moduleName: moduleName,
                        tagData: {
                            obj: obj
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            area: editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai820_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai820_edit',
                                moduleName: moduleName,
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    area: editArea
                                }
                            });
                            break;
                        case 'del':
                            self.deleteDataBatch();
                            break;
                        case 'import':
                            self.reloadUpload();
                            break;
                    }
                });
            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai820_table', where: table_param});
        },
        reloadUpload: function(){
            layui.use('upload', function() {
                var upload = layui.upload;
                upload.render({
                    elem: '#import'
                    ,url: g_ctxPath + 'crm/gwai820/importListData'
                    ,accept: 'file'
                    ,acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                    ,done: function(res, index, upload){
                        if(res.code == 200){
                            app.showMessage("上传成功");
                            self.reloadTableCurr();
                        }else{
                            app.showError(res.message);
                        }
                    }
                    ,error: function(index, upload){
                        app.showError("上传失败");
                    }
                });
            });
        },
        deleteData: function (obj) {
            app.ajax({
                xurl: 'crm/gwai820/deleteData',
                xdata: obj.data,
                loading: true,
                confirm: true,
                okmsg: moduleName + '信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        deleteDataBatch: function () {
            var checkStatus = table.checkStatus('gwai820_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('MMRZ_T');
                    _temp.MMRZENT = v.MMRZENT;
                    _temp.MMRZSEQ = v.MMRZSEQ;
                    _temp.MMRZTYPE = v.MMRZTYPE;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwai820/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: true,
                    okmsg: moduleName + '信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },
        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai820',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'MMRZ001') { //故障内容
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_mmrl001',
                                            columnList: [
                                                {label: '故障编号', prop: 'MMRL001', width: '150'},
                                                {label: '说明', prop: 'MMRLL003', width: '150'},
                                                {label: '助记码', prop: 'MMRLL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRZ001 = data.MMRL001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择故障',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'MMRZ006') {//技术人员
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '维修人员ID', prop: 'ID', width: '150'},
                                                {label: '维修人员账号', prop: 'ACCOUNT', width: '150'},
                                                {label: '维修人员名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRZ006 = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择技术人员',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'MMRZ008') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysorg_id',
                                            columnList: [
                                                {label: '部门编号', prop: 'ID', width: '150'},
                                                {label: '部门名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRZ008 = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择归属部门',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new gwai820();

});
